package Queuedemo;

class Node{
    public int val;
    public Node next;

    //构造方法
    public Node(int val) {
        this.val = val;
    }
}
//单向链表实现
public class MyQueue {
    public Node front;//队头
    public Node rear;//队尾

    //入队(尾插法)
    public void offer(int val){
        Node node = new Node(val);
        if(this.front==null){
            this.front=node;
            this.rear=node;
        }
        this.rear.next = node;
        this.rear = node;
    }
    public boolean isEmpty(){
        if(this.front==null){
            return true;
        }
        return false;
    }
    //返回队头元素
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列为空!");
        }
        return this.front.val;
    }
    //删除队头元素
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空!");
        }
        int old = this.front.val;
        this.front = this.front.next;
        return old;
    }

}
